# 参考资料：
# [1] [腐蚀和膨胀 Erosion/Dilation](https://www.cnblogs.com/sdu20112013/p/11644684.html)
# 说明：通过参考资料[1]可以学会腐蚀和膨胀是如何实现的。
import cv2
import numpy as np

# 1.读取带有毛刺的图片
img = cv2.imread('dog.jpg')
cv2.imshow('dog', img)
cv2.waitKey(0)

# 2. 比较不同的kernel最终的腐蚀效果
kernel = np.ones((3,3), np.uint8)
erosion_1 = cv2.erode(img, kernel, iterations=1)
kernel_1 = np.ones((6, 6), np.uint8)
erosion_2 = cv2.erode(img, kernel_1, iterations=1)
cv2.imshow('erosion', np.hstack((erosion_1, erosion_2)))
cv2.waitKey(0)

# 3. 读取圆的图片
pie = cv2.imread('pie.png')
cv2.imshow('pie', pie)
cv2.waitKey(0)

# 4.比较不同的迭代次数对最终结果的影响
kernel = np.ones((20, 20), np.uint8)
erosion_1 = cv2.erode(pie, kernel, iterations=1)
erosion_2 = cv2.erode(pie, kernel, iterations=2)
erosion_3 = cv2.erode(pie, kernel, iterations=3)
imgs = np.hstack((erosion_1, erosion_2,erosion_3))
cv2.imshow('pie', imgs)
cv2.waitKey(0)
cv2.destroyAllWindows()


def test1():
    img = np.zeros((10, 10, 1), np.uint8)
    img[3:7, 3:7, :] = 255
    img[4:6, 4:6, :] = 200

    kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    erosion_dst = cv2.erode(img, kernel1)
    print(erosion_dst)

test1()
